package com.yun.zf.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yun.zf.common.admin.domain.SysDept;
import com.yun.zf.common.admin.model.sys.DeptSearchMo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SysDeptMapper  extends BaseMapper<SysDept> {

    @Select("<script>" +
            " UPDATE sys_dept set `status`=1 WHERE dept_id =#{deptId} " +
            "</script>")
    public void updateByDeptId(Long deptId);

    @Select("<script>" +
            " select d.dept_id, d.parent_id\n" +
            "\t\tfrom sys_dept d\n" +
            "            left join sys_role_dept rd on d.dept_id = rd.dept_id\n" +
            "        where rd.role_id = #{roleId}\n" +
            "        \tand d.dept_id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = #{roleId})\n" +
            "\t\torder by d.parent_id, d.order_num " +
            "</script>")
    public List<Integer> selectDeptListByRoleId(Long roleId);

    @Select("<script>" +
            "  select d.dept_id, d.parent_id,  d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.create_by, d.create_time \n" +
            "        from sys_dept d WHERE  1=1 " +
            " <if test=\"parentId != null and parentId != 0 \">parent_id = #{parentId},</if> " +
            " <if test=\"deptName != null and deptName != ''\">dept_name like concat('%', #{deptName}, '%') ,</if> " +
            " <if test=\"status != null and status != ''\">status = #{status},</if> " +
            "</script>")
    public List<SysDept> selectDeptList(DeptSearchMo dept);

    @Select("<script>" +
            " UPDATE sys_dept set `status`=#{status} WHERE dept_id =#{deptId} " +
            " <if test=\"parentId != null and parentId != 0 \"> or parent_id = #{parentId}</if> " +
            "</script>")
    public void  changeStatus(Long deptId, int status , Long parentId);
}
